package com.renjibo.sharkerwave.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.renjibo.sharkerwave.R;
import com.renjibo.sharkerwave.models.FirstHand;
import com.renjibo.sharkerwave.network.SharkerParams;
import com.renjibo.sharkerwave.ui.view.GlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 导航页面
 */
public class MainActivity extends BaseActivity {

    //按钮跳转首页
    public void btnClick(View view) {
        startActivity(new Intent(this, HomeActivity.class));
//        startActivity(new Intent(this, PlayVideoActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //创建第一次链接
        FirstHand.getInstance();
        //first请求
        if (FirstHand.isHost()) {
            Log.d("MainActivity", "true");
//            startActivity(new Intent(this, HomeActivity.class));
//            Toast.makeText(this, "isHost true", Toast.LENGTH_SHORT).show();
        } else {
            Log.d("MainActivity", "false");
//            Toast.makeText(this, "isHost false", Toast.LENGTH_SHORT).show();
            firstHand();
        }
        List<String> list = new ArrayList<>();
        list.add("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg");
        list.add("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg");
        list.add("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg");
        list.add("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg");
        Banner main_banner = (Banner) findViewById(R.id.main_banner);
        //设置图片加载器
        main_banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        main_banner.setImages(list);
        //banner设置方法全部调用完毕时最后调用
        main_banner.start();


        FirstHand.getInstance();
        if (FirstHand.isHost()) {
//            DetailTopicActivity.open(this);
        } else {
            firstHand();
        }
    }


    private void firstHand() {
        SharkerParams params = new SharkerParams("first_hand");
        x.http().post(params, new Callback.CommonCallback<FirstHand>() {

            @Override
            public void onSuccess(FirstHand result) {
                FirstHand.save(result);
                Log.d("MainActivity", "result:" + result);
                //请求服务器地址端口
                getHost();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                //TODO 处理网络错误
            }

            @Override
            public void onCancelled(CancelledException cex) {
                //TODO 处理网络错误
            }

            @Override
            public void onFinished() {

            }
        });
    }

    //第二次请求  请求服务器地址端口
    public void getHost() {
        SharkerParams params = new SharkerParams("get_host");
        x.http().post(params, new Callback.CommonCallback<FirstHand>() {
            @Override
            public void onSuccess(FirstHand result) {
                Log.d("MainActivity", "result:" + result);
                FirstHand.saveHost(result);
//                MainActivity.open(SplashActivity.this);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
